package com.cxria.pattern.intermediary

class Mediator extends AbstractMediator {

    @Override
    void execute(String str, Object... objects) {
        switch(str){
            case 'purchase.buy':
                buyComputer(objects[0] as int)
                break
            case 'sale.sell':
                sellComputer(objects[0] as int)
                break
            case 'sale.offsell':
                offSell()
                break
            case 'stock.clear':
                clearStock()
        }
    }

    private void buyComputer(int number){
        if(sale.saleStatus > 80){
            println "采购IBM电脑:${number}台"
            stock.increase(number)
        } else {
            def buyNumber = number/2
            println "采购IBM电脑:${buyNumber}台"
        }
    }

    private void sellComputer(int number){
        if(stock.stockNumber < number){
            purchase.buyIBMcomputer(number)
        }
        stock.decrease(number)
    }

    private void offSell(){
        println "折价销售IBM电脑${stock.stockNumber}台"
    }

    private void clearStock(){
        sale.offSale()
        purchase.refuseBuyIBM()
    }

}